home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / samples.zip / BB4GS.PS < prev    next >
Text File  |  1992-04-23  |  15KB  |  600 lines

  1. %! 
  2. % bb.ps --
  3. % Prints a file, but keeps track of bounding box info, and prints the box at
  4. % the end (around the figure.)
  5. % $Header: bb.ps,v 1.14 91/03/21 13:04:02 cosell Exp $
  6. % RCS log info at end
  7. %
  8. % This version, bb4gs.ps, modified for Ghostscript by William L. Kath
  9. % <kath@nwu.edu>, 92/03/01; all changes clearly marked by `Ghostscript'
  10.  
  11. /strokepath { } def           %% added by wlk for Ghostscript (I think
  12.                               %% the strokepath operator is still not
  13.                               %% fully implemented).
  14.  
  15. %% /show /print load def      %% added by wlk for Ghostscript; uncomment
  16.                               %% this lines to write %%BoundingBox to
  17.                               %% stdout (e.g, for use in a filter to add
  18.                               %% %%BoundingBox to a Postscript file);
  19.                               %% alternatively, add these two lines to
  20.                               %% a separate file and invoke before this
  21.                               %% one when such a filter is desired.
  22.  
  23. 50 dict /$BoundingBox exch def
  24.  
  25. $BoundingBox begin
  26.  
  27. /xdef {
  28.        exch def
  29. } def
  30.  
  31. /xstore {
  32.        exch store
  33. } def
  34.  
  35. /addcoords {
  36.        exch
  37.        4 -1 roll add
  38.        3 1 roll add
  39. } def
  40.  
  41. % Stubs of old functions.
  42.  
  43. /-stroke /stroke load def
  44. /-fill /fill load def
  45. /-eofill /eofill load def
  46. /-image /image load def
  47. /-show /show load def
  48. /-awidthshow /awidthshow load def
  49. /-showpage /showpage load def
  50. /-restore  /restore load def
  51. /-imagemask /imagemask load def
  52.  
  53. end % $BoundingBox
  54.  
  55. % New Functions.   --- These go into the user dict to intercept the calls
  56.  
  57. /stroke {
  58.         $BoundingBox begin
  59.        gsave
  60.        initmatrix
  61.        (stroke called\n) traceprint %%DEBUG
  62.        {
  63.                strokepath      % Make sure to take line width into account.
  64.                0 setlinejoin
  65.                flattenpath
  66.        } stopped {             % strokepath often hits a limitcheck.
  67.                (Can't set up a strokepath\n) traceprint % DEBUG
  68.                grestore        % Restore the original path
  69.                gsave
  70.        } if
  71.        includepath                     % Accumulate it into our box.
  72.        grestore
  73.  
  74.        -stroke
  75.        end % $BoundingBox
  76. } def
  77.  
  78. /fill {
  79.         $BoundingBox begin
  80.        gsave
  81.        (fill called\n) traceprint %%DEBUG
  82.        includepath
  83.        grestore
  84.  
  85.        -fill
  86.        end % $BoundingBox
  87. } def
  88.  
  89. /eofill {
  90.         $BoundingBox begin
  91.        gsave
  92.        (eofill called\n) traceprint %%DEBUG
  93.        includepath
  94.        grestore
  95.  
  96.        -eofill
  97.        end % $BoundingBox
  98. } def
  99.  
  100. % Text is implemented by reducing everything to an `awidthshow'.
  101.  
  102. /show {
  103.         $BoundingBox begin
  104.        (show called\n) traceprint %%DEBUG
  105.        0 0 0 0 0               % Extra parameters for awidthshow
  106.        6 -1 roll               % Bring the string back up
  107.        awidthshow
  108.        end % $BoundingBox
  109. } def
  110.  
  111. /widthshow {
  112.         $BoundingBox begin
  113.         (widthshow called\n) traceprint %%DEBUG
  114.        0 0                     % Extra parameters for awidthshow
  115.        3 -1 roll               % Bring the string back up.
  116.        awidthshow
  117.        end % $BoundingBox
  118. } def
  119.  
  120. /ashow {
  121.         $BoundingBox begin
  122.         (ashow called\n) traceprint %%DEBUG
  123.        0 0 0 
  124.        6 3 roll
  125.        awidthshow
  126.        end % $BoundingBox
  127. } def
  128.  
  129.  
  130. % This does all of the work of the text-rendering operators
  131. %   What it does, is compute, basically brute force, what 'charpath'
  132. %   would have given us virtually for free, if 'show' were the only
  133. %   operator that we needed to do.
  134.  
  135. /awidthshow {
  136.        $BoundingBox begin
  137.        gsave
  138.        6 (awidthshow:) debug %%DEBUG
  139.        currentpoint
  140.        2 copy /@starty xdef /@startx xdef
  141.        2 index stringwidth     % Get the natural length of the string
  142.        addcoords                       % Add to the start to get the end.
  143.  
  144.        2 index length          % How many characters?
  145.  
  146.        dup                     % Add the offsets to each character
  147.        6 index mul
  148.        exch 5 index mul
  149.        addcoords
  150.  
  151.        5 index 3 index
  152.        chcount         % How many padding characters?
  153.  
  154.        dup                     % Add the offsets for each pad.
  155.        9 index mul
  156.        exch 8 index mul
  157.        addcoords
  158.  
  159.        /@endy xdef /@endx xdef
  160.  
  161.        % We now have the left and right edges (in user coords)
  162.        % of the text.  Now we need only correct for the vertical
  163.        % displacements needed for the font and we can get the
  164.        % top and bottom edges of the enclosing box
  165.  
  166.        fontheight              % Get the height and depth of the current font.
  167.        
  168.        @startx @starty addcoords
  169.        /@starty xdef /@startx xdef
  170.        @endx @endy addcoords
  171.        /@endy xdef /@endx xdef
  172.        newpath
  173.        @startx @starty moveto
  174.        @endx @starty lineto
  175.        @endx @endy lineto
  176.        @startx @endy lineto
  177.        closepath
  178.         includepath
  179.        grestore
  180.  
  181.        -awidthshow
  182.        end % $BoundingBox
  183. } def
  184.  
  185. % `image':
  186. % Assume here that the image lands in the unit square.
  187.  
  188. /image {
  189.         $BoundingBox begin
  190.         (image called\n) traceprint %%DEBUG
  191.        gsave
  192.        newpath
  193.        0 0 moveto
  194.        1 0 rlineto
  195.        1 1 rlineto
  196.        -1 0 rlineto
  197.        closepath
  198.        includepath
  199.        grestore
  200.  
  201.        -image
  202.        end % $BoundingBox
  203. } def
  204.  
  205. /imagemask
  206. {
  207.     $BoundingBox begin
  208.     (imagemask called\n) traceprint %%DEBUG
  209.     gsave
  210.     newpath
  211.     0 0 moveto
  212.     1 0 rlineto
  213.     1 1 rlineto
  214.     -1 0 rlineto
  215.     closepath
  216.     includepath
  217.     grestore
  218.  
  219.     -imagemask
  220.     end % $BoundingBox
  221. } def
  222.  
  223. % Just define this one out of existence
  224. /framedevice { pop pop pop pop } def
  225.  
  226. % Handle restoring VM --- this is all OK, except that we have to
  227. % hang onto the bb info we collected while in the about-to-be-discarded
  228. % environment
  229.  
  230. /restore
  231. {
  232.     $BoundingBox begin
  233.     (restore called\n) traceprint %%DEBUG
  234.     tracedump  %% HACK, but the only way I see right now to get this stuff!
  235.     bbox-llx bbox-lly bbox-urx bbox-ury 
  236.     5 -1 roll
  237.     -restore
  238.     /bbox-ury xstore /bbox-urx xstore
  239.     /bbox-lly xstore /bbox-llx xstore
  240.     end % $BoundingBox
  241. } def
  242.  
  243.     
  244. % `showpage':
  245. % Just draw the box around the figure and print the page, and then initialize
  246. % the bounding box variables again.
  247.  
  248. $BoundingBox begin
  249. /temp-string 10 string def
  250. end % $BoundingBox
  251.  
  252. /showpage {
  253.        $BoundingBox begin
  254.        initgraphics
  255.  
  256.         (showpage\n) traceprint % DEBUG
  257.        dump-bbox  % DEBUG
  258.  
  259.         /bbox-llx round_down
  260.        /bbox-lly round_down
  261.        /bbox-ury round_up
  262.        /bbox-urx round_up
  263.  
  264.        bbox-llx bbox-lly moveto                % Make the box
  265.        bbox-llx bbox-ury lineto
  266.        bbox-urx bbox-ury lineto
  267.        bbox-urx bbox-lly lineto
  268.        closepath
  269.  
  270.        bwstroke                        % Draw the box.
  271.  
  272. % Print the size of the bounding box both above and below the actual box
  273.        0 setgray
  274.   %%    /Courier findfont 10 scalefont setfont
  275.   %%    bbox-llx 36 max bbox-lly 12 sub 36 max moveto
  276.         /Courier findfont 25 scalefont setfont         %% changed by wlk
  277.         bbox-llx 36 max bbox-lly 29 sub 36 max moveto  %% for Ghostscript
  278.        (%%BoundingBox: ) -show
  279.        bbox-llx temp-string cvs -show ( ) -show
  280.        bbox-lly temp-string cvs -show ( ) -show
  281.        bbox-urx temp-string cvs -show ( ) -show
  282.        bbox-ury temp-string cvs -show
  283.  
  284.        bbox-llx 36 max bbox-ury 12 add 740 min moveto
  285.         /-show load /print load ne       %% added by wlk for Ghostscript
  286.         {                                %% added by wlk for Ghostscript
  287.         (%%BoundingBox: ) -show
  288.        bbox-llx temp-string cvs -show ( ) -show
  289.        bbox-lly temp-string cvs -show ( ) -show
  290.        bbox-urx temp-string cvs -show ( ) -show
  291.        bbox-ury temp-string cvs -show
  292.         } if                             %% added by wlk for Ghostscript
  293.  
  294.        init
  295.        -showpage
  296.